package com.example.ymm.modules1.controller.common;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.example.ymm.exception.YmException;
import com.example.ymm.model.CommonResult;
import com.example.ymm.modules1.entity.my.sys.DbTest;
import com.example.ymm.modules1.service.dto.DbTestQueryCriteria;
import com.example.ymm.modules1.service.dto.heng.SubmitXftDataDto;
import com.example.ymm.modules1.service.sys.DbTestService;
import com.example.ymm.util.FileUtil;
import com.example.ymm.util.RedisUtil;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.Valid;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/hl/test")
@Tag(name ="dbtest接口")
@Slf4j
public class DbTestController {


    @Autowired
    DbTestService dbTestService;

    @Autowired
    RedisUtil redisUtil;

    @Schema( name =  "列表")
    @RequestMapping(value = "/list", method = RequestMethod.POST)
    public CommonResult list(DbTestQueryCriteria criteria) {
        try{
            List<DbTest> dbTests = dbTestService.queryAll(criteria);
            return CommonResult.success(dbTests);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Schema( name =  "新增")
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public CommonResult add(@Valid DbTest dbTest) {
        try{

            dbTestService.saveOrUpdate(dbTest);
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Schema( name =  "部门抽签")
    @RequestMapping(value = "/departRandom", method = RequestMethod.POST)
    public CommonResult departRandom(String userName) {
        try{

            List<String> list = dbTestService.departRandom(userName);
            return CommonResult.success(list);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Schema( name =  "清除抽中结果")
    @RequestMapping(value = "/departClear", method = RequestMethod.POST)
    public CommonResult departClear() {
        try{
            dbTestService.departClear();
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Schema( name =  "go")
    @RequestMapping(value = "/go", method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult go() {
        try{
            return CommonResult.success("2024-08-21 17：29:35");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Schema( name =  "up1")
    @RequestMapping(value = "/up1", method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult up1(MultipartFile file) {
        try{
            String str =FileUtil.uploadImgToLocal(file);
            return CommonResult.success(str);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

    @Schema( name =  "upMp3")
    @RequestMapping(value = "/upMp3", method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult upMp3() {
        try{
            Timestamp st=new Timestamp(new Date().getTime());
            String ogg="https://static.dingtalk.com/media/lR_PD298BnsLiw0AALAg_JzMB9Hd2AZlUWoJ-uAA.ogg";
            String str =FileUtil.uploadOggFn(ogg,st);
            return CommonResult.success(str);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Schema( name =  "testError")
    @RequestMapping(value = "/testError", method = {RequestMethod.POST,RequestMethod.GET})
    public CommonResult testError() {
        try{
            throw new YmException("测试异常aaa");
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Schema( name =  "ms数据更新通知")
    @RequestMapping(value = "/changeNotify", method = {RequestMethod.POST,RequestMethod.GET})

    public CommonResult changeNotify(String id,String name) {
        try{
            System.out.println("--id:"+id);
            System.out.println("--name:"+name);
            return CommonResult.success(id);
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }


    @Schema( name =  "测试redis")
    @RequestMapping(value = "/redis_set", method = RequestMethod.POST)
    public CommonResult redis_set() {
        try{
            SubmitXftDataDto dataDto = new SubmitXftDataDto();
            dataDto.setTotalPrice(new BigDecimal(123));
            dataDto.setDeptName("test");
            dataDto.setUserPrice(new BigDecimal(78));
            dataDto.setUserName("刘庆峰");
            dataDto.setApproveNo("YW_000WEWQE1");

            List<SubmitXftDataDto> arr=new ArrayList<>();
            arr.add(dataDto);
            String jsonString = JSONObject.toJSONString(arr);

            redisUtil.set("redis_test",jsonString);
//            redisUtil.hmGet(redis_set)
//            redisUtil.hmGet("redis_set")
            return CommonResult.success();
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }



    @Schema( name =  "测试redis")
    @RequestMapping(value = "/redis_get", method = RequestMethod.POST)
    public CommonResult redis_get() {
        try{
            String s = redisUtil.getString("redis_test");
//            List<Object> o1 = redisUtil.hmGet("redis_set", keys);
            return CommonResult.success(JSONArray.parseArray(s));
        }catch (Exception e){
            log.error(e.getMessage(),e);
            return CommonResult.error(e.getMessage());
        }
    }

}
